var Dialog = (function(window, document){
	
	var body, // Body of the dialog
		mes, // The message Container
		bts, //Button container
		curBts; //Current buttons
	
	function init(){
		body = js.dom.Helper.append(js.dom.BODY,{
			"class":"jsool-popup",
			children:[{
				children:[{
					tag: "p",
					"className":"popup-message no-select"
				},{
					tag:"p",
					"className":"popup-buttons"
				}]
			}],
			className: "jsool-popup"
		});
		
		mes = jsool.get(Raze.queryNode(".popup-message", body));
		bts = jsool.get(Raze.queryNode(".popup-buttons", body));
		body = jsool.get(body);
		body.applyStyle({
			display: "none"
		});
	}
	
	function hide(){
		if(curBts && curBts.length > 0){
		
			Array.iterate(curBts,function(index, item){
				item.destroy();
			});
			
			curBts = null;
		}
		
		body.applyStyle({
			display: "none"
		});
	}
	
	function showBox(config){
		if(!body)init();
		hide();
		
		body.applyStyle("display","block");
		
		mes.setText(config.message);
		
		
		if(!config.position){
			var win = jsool.windowSize();
			
			var w = win.width, h = win.height;
			
			var b = body.getBox();
			
			body.applyStyle({
				top: (h / 2) - (b.h / 2) + "px",
				left:(w / 2) - (b.w / 2) + "px"
			});
		}else{
			body.applyStyle({
				top: config.position[1] + "px",
				left:config.position[0] + "px"
			});
		}
		
		if(config.buttons){
			var h = js.dom.Helper;
			curBts = [];
			if(Array.isArray(config.buttons)){
			}else{
				var bt, btName;
				
				for(btName in config.buttons){
					bt = h.append(bts,{
						tag: "button",
						html: btName
					});
					
					bt = jsool.get(bt);
					var fn = config.buttons[btName];
					
					function enclose(e){
						var f = fn;
						return function(e){
							hide();
							return f(e);
						};
					}
					
					bt.on("click",enclose());
					
					curBts.push(bt);
				}
			}
		}
	}
	
	return {
		alert: function(text){
			if(!body)init();
			hide();
			
			showBox({
				message: text,
				buttons : {
					"OK": function(ev){
						return true;
					}
				}
			});
		},
		hide: hide,
		show: showBox
	};
})(window, document);